package logic

import (
	"context"
	"encoding/json"
	"hzjt-oam/rpc/sys/internal/svc"
	"hzjt-oam/rpc/sys/pb"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchSysUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchSysUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchSysUserLogic {
	return &SearchSysUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SearchSysUserLogic) SearchSysUser(in *pb.SearchSysUserReq) (*pb.SearchSysUserResp, error) {
	all, err := l.svcCtx.UserModel.FindAll(in.Page, in.PageSize)

	if err != nil {
		reqStr, _ := json.Marshal(in)
		logx.WithContext(l.ctx).Errorf("查询用户列表信息失败,参数:%s,异常:%s", reqStr, err.Error())
		return nil, err
	}

	var list []*pb.SysUser
	for _, user := range all {
		list = append(list, &pb.SysUser{
			Id:             user.Id,
			Name:           user.Name,
			NickName:       user.NickName,
			Avatar:         user.Avatar,
			Password:       user.Password,
			Salt:           user.Salt,
			Email:          user.Email,
			Mobile:         user.Mobile,
			Status:         user.Status,
			DeptName:       user.DeptName,
			CreateBy:       user.CreateBy,
			CreateTime:     time.Now().Unix(),
			LastUpdateBy:   user.CreateBy,
			LastUpdateTime: time.Now().Unix(),
			DelFlag:        user.DelFlag,
			RoleName:       user.RoleName,
		})
	}

	reqStr, _ := json.Marshal(in)
	listStr, _ := json.Marshal(list)
	logx.WithContext(l.ctx).Infof("查询用户列表信息,参数：%s,响应：%s", reqStr, listStr)

	return &pb.SearchSysUserResp{
		SysUser: list,
	}, nil
}
